#include "mainwindow.h"
#include "CameraPlayer.h"
#include "gst/gstbin.h"
#include "gst/gstelement.h"
#include "qboxlayout.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QTimer>

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    pipeline = nullptr;
    ui->setupUi(this);
    QHBoxLayout* lo = new QHBoxLayout(this->centralWidget());
    QLabel* center = new QLabel(this);
    m_wid = center->winId();
    lo->addWidget(center);
    QVBoxLayout* vlo = new QVBoxLayout;
    lo->addLayout(vlo);

    CameraPlayer* cp = new CameraPlayer(center);
    cp->open("/dev/video0");

    QPushButton* pb = new QPushButton("open", this);
    vlo->addWidget(pb);
    connect(pb, &QPushButton::clicked, this, [=]() {
        cp->open("/dev/video0");
        if (!pipeline) {
            qDebug() << "pipeline is nullptr";
            return;
        }
        gst_element_set_state(pipeline, GST_STATE_PLAYING);
    });
    QPushButton* pbclose = new QPushButton("close", this);
    vlo->addWidget(pbclose);
    connect(pbclose, &QPushButton::clicked, this, [=]() {
        cp->close();
        if (!pipeline) {
            qDebug() << "pipeline is nullptr";
            return;
        }
        gst_element_set_state(pipeline, GST_STATE_NULL);
        center->setText("Stoped Camera");
    });

    QPushButton* mirrored = new QPushButton("mirror", this);
    vlo->addWidget(mirrored);
    connect(mirrored, &QPushButton::clicked, this, [=]() {
        cp->mirred();
        toggleHorizontalMirror();
    });

    // gst_init(nullptr, nullptr); // 初始化 GStreamer
    // createPipeline();
}

MainWindow::~MainWindow()
{
    if (pipeline) {
        qDebug() << __func__;
        gst_element_set_state(pipeline, GST_STATE_NULL);
        gst_object_unref(pipeline);
    }
    delete ui;
}

void MainWindow::createPipeline()
{
    // 创建 GStreamer 管道
    pipeline = gst_parse_launch("v4l2src device=/dev/video0 ! "
                                "video/x-raw, width=640, height=480, framerate=30/1 ! "
                                "videoconvert ! videoflip name=videoFlip method=none ! glimagesink name=sink",
        nullptr);

    if (!pipeline) {
        qFatal("Failed to create GStreamer pipeline");
    }

    flip = gst_bin_get_by_name(GST_BIN(pipeline), "videoFlip");
    if (!flip) {
        qDebug() << "failed to get videoflip element";
    }

    // 获取 sink 元素并设置窗口句柄
    GstElement* sink = gst_bin_get_by_name(GST_BIN(pipeline), "sink");
    if (sink) {
        GstVideoOverlay* overlay = GST_VIDEO_OVERLAY(sink);
        gst_video_overlay_set_window_handle(overlay, (guintptr)m_wid);
    } else {
        qFatal("Failed to get sink element");
    }

    // 启动管道
    gst_element_set_state(pipeline, GST_STATE_PLAYING);
}

void MainWindow::toggleHorizontalMirror()
{
    // 切换水平镜像状态
    if (mirrorHorizontal) {
        // 关闭水平镜像
        g_object_set(G_OBJECT(flip), "method", 0, nullptr);
        QPushButton* button = qobject_cast<QPushButton*>(sender());
        button->setText("Toggle Horizontal Mirror");
    } else {
        // 开启水平镜像
        g_object_set(G_OBJECT(flip), "method", 4, nullptr);
        QPushButton* button = qobject_cast<QPushButton*>(sender());
        button->setText("Toggle Horizontal Mirror");
    }
    mirrorHorizontal = !mirrorHorizontal;
}
